package demo2;

import java.util.Arrays;

public class MyArrayList {
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_SIZE = 4;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2*this.elem.length);
        }

        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    private boolean checkPosInAdd(int pos){
        if (pos < 0 || pos > this.usedSize) {
            throw new MyArrayListIndexOutOfException("pos位置不合法！");
        }
        return true;//合法
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if (!checkPosInAdd(pos)) {
            return;
        }

        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2*this.elem.length);
        }

        int i = this.usedSize-1;
        while (i >= pos) {
            this.elem[i+1] = this.elem[i];
            i--;
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    private boolean checkPosInGet(int pos){
        if (pos < 0 || pos >= this.usedSize) {
            return false;
        }
        return true;//合法
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if (!checkPosInGet(pos)) {
            throw new MyArrayListIndexOutOfException("pos位置不合法！");
        }

        return this.elem[pos];
    }

    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        if (!checkPosInGet(pos)) {
            throw new MyArrayListIndexOutOfException("pos位置不合法！");
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if (this.elem == null) {
            System.out.println("顺序表为空！");
            return;
        }
        int index = indexOf(toRemove);
        if (index == -1) {
            System.out.println("不存在你要删除的数据！");
            return;
        }

        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

    // 获取顺序表长度
    public int size() {
        return this.elem.length;
    }

    // 清空顺序表
    public void clear() {
        // 如果是引用类型，需要一个个置空
//        for (int i = 0; i < this.usedSize; i++) {
//            this.elem[i] = null;
//        }
        this.usedSize = 0;
    }
}
